package com.ruoyi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.pojo.Fund;
import com.ruoyi.pojo.Investment;
import com.ruoyi.service.FundService;
import com.ruoyi.service.InvestmentService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.List;

@RestController
@RequestMapping("/bid")
public class BidController {

    @Autowired
    private FundService fundService;

    @Autowired
    private InvestmentService investmentService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //查询所有标数据
    @PostMapping("/fundlist")
    public List<Fund> fundList(){
        List<Fund> list = fundService.list();
        return list;
    }

    //计算投标收益
    @PostMapping("/fundinterest")
    public BigDecimal fundInterest(@RequestBody Investment investment){
        QueryWrapper<Fund> fundQueryWrapper = new QueryWrapper<>();
        fundQueryWrapper.eq("fid",investment.getFid());
        List<Fund> fundList = fundService.list(fundQueryWrapper);
        Fund fund = fundList.get(0);
        Double interestrate = fund.getInterestrate() * 0.0001;
        BigDecimal b1 = new BigDecimal(interestrate);
        MathContext mathContext = new MathContext(2);
        BigDecimal multiply = investment.getImoney().multiply(b1, MathContext.DECIMAL32);
        return multiply;
    }

    //对标进行投资
    @PostMapping("invest")
    public void invest(@RequestBody Investment investment){
        BigDecimal b1 = new BigDecimal("0");
        investmentService.save(investment);
        if (b1.subtract(investment.getImoney()).compareTo(BigDecimal.ZERO) != 1){
            rabbitTemplate.convertAndSend("seckillQueue",investment.getIid());
        }
    }

    @PostMapping("investa")
    public void investa(@RequestParam("iid") Integer iid){
        rabbitTemplate.convertAndSend("user",iid);
    }
}
